
import store from '@/store'
import Commonjs from './common'
export default {
  mixins: [Commonjs],
  data () {
    return {
      id: '', // 表ID
      loading: false, // 加载
      searchParams: {}, // 查询条件参数
      list: [], // 数据集合
      totalRow: 0, // 总行数
      pageSize: 10, // 每页行数
      currentPage: 1 // 当前页,
    }
  },
  computed: {
    productTypes () {
      let pTypes = this.$store.state.common.productTypes || []
      if (!pTypes || pTypes.length <= 0) {
        store.dispatch('common/GetProductType').then(res => {
          pTypes = res && res !== null ? res.data : []
        })
      }
      return pTypes
    },
    serviceTypes () {
      let serviceTypes = this.$store.state.common.serviceTypes || []
      if (!serviceTypes || serviceTypes.length <= 0) {
        store.dispatch('common/GetServiceType').then(res => {
          serviceTypes = res && res !== null ? res.data : []
        })
      }
      return serviceTypes
    }
  },
  methods: {
    initPageList (opt) {
      this.pageList({currentPage: this.currentPage, pageSize: this.pageSize}, opt)
    },
    /**
     * 分页查询
     * @param pageInfo
     */
    pageList (pageInfo, opt) {
      let requestAPI
      if (opt && opt !== undefined && typeof opt === 'function') {
        requestAPI = opt
      } else {
        requestAPI = this.getRequestType()
      }
      if (!requestAPI) {
        return
      }
      const params = this.searchParams ? {...this.searchParams} : {}
      params.pageNumber = pageInfo.currentPage
      params.pageSize = pageInfo.pageSize
      // console.log('请求参数：', params)
      this.loading = true
      const vm = this
      requestAPI(params).then(res => {
        setTimeout(() => {
          vm.loading = false
          vm.list = res.data && res.data !== null ? res.data.list : []
          vm.totalRow = res.data && res.data !== null ? res.data.totalRow : 0
        }, 800)
        vm.$nextTick(() => {
          // 滚动条 回到初始状态
          document.querySelector('.el-table__body-wrapper').scrollTop = 1
        })
      }).catch(error => {
        console.log(error)
        vm.loading = false
      })
    },
    /**
     * 分页查询 条件查询
     * @param searchParams
     */
    search (searchParams, opt) {
      this.searchParams = searchParams || {}
      this.initPageList(opt)
    },

    /**
     *  格式化展示产品类型
     * @param row
     * @param column
     * @param cellValue
     * @param index
     * @returns {*}
     */
    formatterProductType (row, column, cellValue, index) {
      const productTypes = this.productTypes
      // const productTypes = this.$store.state.common.productTypes
      if (productTypes && productTypes.length > 0) {
        const productTypeInfo = productTypes.find((item) => {
          if (cellValue === item.bizValue) {
            return item
          }
        })
        return productTypeInfo && productTypeInfo !== null ? productTypeInfo.bizName : cellValue
      }
      return cellValue
    }
  }
}
